home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7973 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  86 lines

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: gnu.g++,gnu.g++.help,comp.lang.c++
  4. Subject: Re: Overloaded new operator in C++
  5. Date: Fri, 16 Feb 1996 10:39:03 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <312450B7.167EB0E7@intellektik.informatik.th-darmstadt.de>
  8. References: <4g0boe$c37@maverick.tad.eds.com>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. User Jdsmith wrote:
  16. > I am experiencing a problem with overloading the
  17. > new operator when allocating an array of classes.
  18. > I do the following and my overloaded new operator is
  19. > called as expected:
  20. > ptr = new ClassThatOverloadsNew;
  21. > But when I do this:
  22. > ptr = new ClassThatOverloadsNew[5];
  23. > The default new operator is called.
  24. > What needs to be done to get an overloaded new operator to be called
  25. > when allocating an array of data types?  Does C++ even allow this
  26. > to be done?  I have checked several C++ manuals and none explicitely
  27. > state that this can be done (and none say that it cannot be done!).
  28. > I assumed that the overloaded new operator would be called with the
  29. > number of bytes (the first parameter) being set to:
  30. > sizeof(class) * arrayCount
  31. > But this is not the case.  Does anybody know how to solve this problem?
  32. > I'm sure it is supported and is simple to do, I just have not found the
  33. > right documentation on how to do it.  My assumptions have been wrong so
  34. > far, so I need some help.
  35.  
  36. You have to provide array new/delete operators.
  37. For instance, the subsequent code shows how to overload the appropriate
  38. allocation functions for a class 'A':
  39.  
  40. >>>>
  41. #include <iostream.h>
  42. #include <new.h>
  43.  
  44. struct A {
  45.   void* operator new (size_t t) { 
  46.     cout << "new\n"; 
  47.     return :: operator new(t); 
  48.   }
  49.   void* operator new[] (size_t t) { 
  50.     cout << "new[]\n"; 
  51.     return :: operator new[](t); 
  52.   }
  53.   void operator delete(void* p) { 
  54.     cout << "delete\n"; 
  55.     :: operator delete(p); 
  56.   }
  57.   void operator delete[](void* p) {
  58.     cout << "delete[]\n"; 
  59.     :: operator delete(p); 
  60.   }
  61. };
  62.  
  63. int main()
  64. {
  65.   delete new A;
  66.   delete[] new A[5];
  67.   return 0;
  68. }
  69. <<<<
  70.  
  71. When an array of objects of type 'A' is created 'A:: operator new[](size_t)' is called.
  72. 'A:: operator delete[](size_t)' is used to release the allocated memory when the array
  73. is destructed.
  74.  
  75.     Enno
  76.